//
//  VideoTableViewCell.m
//  SuperNews
//
//  Created by qiang on 16/1/14.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "VideoTableViewCell.h"
#import "VideoPageModel.h"

@implementation VideoTableViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cell";
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[VideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    
    self.bgView  = [[UIView alloc] initWithFrame:CGRectMake(8, 5, SCREEN_WIDTH - 16, 285)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    //给bgView边框设置阴影
    self.bgView.layer.shadowOffset = CGSizeMake(2,2);
    self.bgView.layer.shadowOpacity = 0.5;
    self.bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    [self.contentView addSubview:self.bgView];
    
    //题目
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 30)];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
 
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH - 20, 20)];
    descLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:descLabel];
    descLabel.textColor = [UIColor grayColor];
    descLabel.font = [UIFont systemFontOfSize:14];
    self.descLabel = descLabel;
    
    //图片
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, SCREEN_WIDTH - 20, 200)];
    [self.contentView addSubview:imageview];
    self.imageview = imageview;
    
    UIImageView *playcoverImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 100, 100, 100)];
    [self.contentView addSubview:playcoverImage];
    self.playcoverImage = playcoverImage;
    
    //时长
    UILabel *lengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 80, 40)];
    lengthLabel.textColor = [UIColor grayColor];
    lengthLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:lengthLabel];
    self.lengthLabel = lengthLabel;
    
    //播放数
    UILabel *playcountLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100, 40)];
    playcountLabel.textColor = [UIColor grayColor];
    playcountLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:playcountLabel];
    self.playcountLabel = playcountLabel;
    
    //时间
    UILabel *ptimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 250, 150, 40)];
    ptimeLabel.textColor = [UIColor grayColor];
    ptimeLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:ptimeLabel];
    self.ptimeLabel = ptimeLabel;
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 298, SCREEN_WIDTH, 2)];
    lineV.backgroundColor = [UIColor colorWithRed:0.776 green:0.750 blue:0.783 alpha:1.000];
    [self.contentView addSubview:lineV];
    self.lineV = lineV;

}


- (void)setVideodata:(VideoPageModel *)videodata
{
    _videodata = videodata;
    //题目
    NSString *str = [videodata.title stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    self.titleLabel.text = str;
    self.descLabel.text = videodata.Description;
    //图片
    [self.imageview sd_setImageWithURL:[NSURL URLWithString:videodata.cover] placeholderImage:nil];
    self.playcoverImage.image = [UIImage imageNamed:@"play120"];
    //时长
    self.lengthLabel.text = [NSString stringWithFormat:@"🕒 %@", [self convertTime:videodata.length]];
    //播放数
    self.playcountLabel.text = [NSString stringWithFormat:@"💿 %ld次", videodata.playCount];
    //时间
    self.ptimeLabel.text = videodata.ptime;
}

//时间转换
- (NSString *)convertTime:(CGFloat)second
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    if (second/3600 >= 1) {
        [fmt setDateFormat:@"HH:mm:ss"];
    } else {
        [fmt setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [fmt stringFromDate:d];
    return showtimeNew;
}


@end
